/**
 * Copyright 2006 Brad Reynolds
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.leeloo.databinding.internal.ui;

import junit.framework.TestCase;

import org.leeloo.core.Tag;
import org.leeloo.core.TagService;
import org.leeloo.core.Tags;
import org.leeloo.databinding.ui.event.AbstractEvent;
import org.leeloo.databinding.ui.event.IEvent;
import org.leeloo.internal.core.ObjectDescriptor;

public class LabeledTreeNodeLabelProviderTest extends TestCase {
    private LabeledTreeNodeLabelProvider labelProvider;

    private TagService tagService;

    protected void setUp() throws Exception {
        tagService = new TagService();
        labelProvider = new LabeledTreeNodeLabelProvider(tagService);
    }

    public void testGetName() throws Exception {
        String name = "name";

        LabeledTreeNode node = new LabeledTreeNode("");
        node.setLabel(name);
        LabeledTreeNodeLabelProvider labelProvider = new LabeledTreeNodeLabelProvider(tagService);
        assertEquals(name, labelProvider.getColumnText(node, 0));
    }

    public void testGetTextOfObjectDescriptor() throws Exception {
        ObjectDescriptor descriptor = new ObjectDescriptor();

        LabeledTreeNode node = new LabeledTreeNode(descriptor);
        node.setLabel("");
        assertNull(labelProvider.getColumnText(node, 1));
    }

    public void testGetTextOfIEvent() throws Exception {
        IEvent event = new AbstractEvent() {
        };

        LabeledTreeNode node = new LabeledTreeNode(event);
        node.setLabel("");
        assertNull(labelProvider.getColumnText(event, 1));
    }

    public void testGetTextOfNullValue() throws Exception {
        LabeledTreeNode node = new LabeledTreeNode(null);
        node.setLabel("");
        assertEquals(LabeledTreeNodeLabelProvider.NO_VALUE, labelProvider.getColumnText(node, 1));
    }

    public void testGetTagForLabeledTagTreeNode() throws Exception {
        ObjectDescriptor descriptor = new ObjectDescriptor();
        String tagValue = "tag value";
        String label = "label";

        tagService.put(descriptor, new Tag(Tags.NAME, tagValue));
        LabeledTagTreeNode node = new LabeledTagTreeNode(descriptor, new Tag(Tags.NAME, ""), true);
        node.setLabel(label);

        assertEquals(label, labelProvider.getColumnText(node, 0));
        assertEquals(tagValue, labelProvider.getColumnText(node, 1));
    }

    public void testLabeledTagTreeNodeNoTag() throws Exception {
        ObjectDescriptor descriptor = new ObjectDescriptor();

        LabeledTagTreeNode node = new LabeledTagTreeNode(descriptor, new Tag(Tags.NAME, ""), true);
        assertEquals(LabeledTreeNodeLabelProvider.SELECT_TO_EDIT, labelProvider.getColumnText(node, 1));
    }

    public void testLabeledTagTreeNodeEmptyTag() throws Exception {
        ObjectDescriptor descriptor = new ObjectDescriptor();

        tagService.put(descriptor, new Tag("name", ""));
        LabeledTagTreeNode node = new LabeledTagTreeNode(descriptor, new Tag(Tags.NAME, ""), true);
        assertEquals(LabeledTreeNodeLabelProvider.SELECT_TO_EDIT, labelProvider.getColumnText(node, 1));
    }
}